<?php

/*
 * litchicms
 * Copyright (C) 2010-5-26 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");
check_rights_by_id(24);
$can_action = array('show', 'update');

if (empty($_POST['action']) || !in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'show';
}

switch ($_POST['action']) {
    case 'show':
        $RIGHT = array();
        if ($BRK_USER['group_id'] == 65535) {
            write_conf_file(65535);
            $rs = brk_select("right");
            while ($row = $Db->fetch_array($rs)) {
                $RIGHT[$row['right_id']] = $row;
            }
        } else {
            foreach ($BRK_GLOBAL['RIGTHS']['node'] as $k => $v) {
                if ($k == -2) {
                    continue;
                }
                $RIGHT[$k] = array(
                    'right_id' => $k,
                    'right_pid' => $BRK_GLOBAL['RIGTHS']['parent'][$k],
                    'right_node' => $v,
                    'right_note' => $BRK_GLOBAL['RIGTHS']['note'][$k]
                );
            }
        }
        !class_exists('Tree') && require 'tree.class.php';
        $tree = new Tree($RIGHT, 'right_id', 'right_pid', 'right_note');
//        dump($tree);
        $RIGHT_TREE = $tree->getChilds();
        array_shift($RIGHT_TREE);
        $RIGHT_TREE = array_flip($RIGHT_TREE);

        //下面的这段代码把我心都写痛了.....不过总数弄出来了
        foreach ($RIGHT_TREE as $k => &$v) {
            $v = array($k, $tree->getValue($k), $tree->getLayer($k, "", 1), '', 'is_end' => FALSE, 'pid');
            $l = count($tree->getChilds($k));
            $thisparent = $tree->getParent($k);
            $v['pid'] = $thisparent;
            $is_parent = $tree->getChild($k) ? TRUE : FALSE;
            $this_pre = $is_parent ? "<img src='system/data/treegif/20.gif' />" : "<img src='system/data/treegif/19.gif'   />";
            $thisonclick = $is_parent ? " onclick=\"hidechilds(" . ($l - 1) . ")\" " : "";
            $is_end = $k == array_pop($tree->getChild($thisparent)) ? TRUE : FALSE;
            $is_end && $v['is_end'] = TRUE;
            $this_node = $is_parent ? ($is_end ? 17 : 12) : ($is_end ? 16 : 14);
            if (!key_exists($thisparent, $RIGHT_TREE)) {
                $v[3] = "";
            } else {
                $v[3].=$RIGHT_TREE[$thisparent][3] . ($RIGHT_TREE[$thisparent]['is_end'] ? "<img src='system/data/treegif/18.gif'/>" : "<img src='system/data/treegif/13.gif'/>");
            }
            $this_node = $v[3] . "<img src='system/data/treegif/" . $this_node . ".gif' " . $thisonclick . "/>" . $this_pre;
            $v[2] = $this_node;
        }

        break;
    default:
        break;
}
?>